package com.xy.manage.action;



import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.octo.captcha.service.CaptchaServiceException;
import com.octo.captcha.service.image.ImageCaptchaService;
import com.xy.common.page.Pagination;
import com.xy.common.util.DictUtils;
import com.xy.common.security.BadCredentialsException;
import com.xy.common.security.UsernameNotFoundException;
import com.xy.common.web.CookieUtils;
import com.xy.common.web.RequestUtils;
import com.xy.core.web.WebErrors;

import com.xy.manage.model.Tcomplaint;
import com.xy.manage.service.TcomplaintService;



@Controller
public class TcomplaintAction{
	private static final Logger log = LoggerFactory.getLogger(TcomplaintAction.class);
	public TcomplaintAction() {
	}
 
 
 	/**
	 * 民意警务网上投诉新增页面
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tcomplaintAdd.do")
	public String add(HttpServletRequest request, ModelMap model) {
		model.addAttribute("tcomplaint", new Tcomplaint());
        setDict(model);
		return "tcomplaintEdit";
	}

	/**
	 * 民意警务网上投诉新增保存
	 * 
	 * @param tcomplaint
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/o_tcomplaintSave.do")
	public String save(Tcomplaint tcomplaint, HttpServletRequest request, ModelMap model) {
		WebErrors errors = validateSave(tcomplaint, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		tcomplaint = tcomplaintService.save(tcomplaint);
		return "redirect:v_tcomplaintList.do";
	}

	/**
	 * 民意警务网上投诉删除
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping("o_tcomplaintDelete.do")
	public String delete(long[] ids) {
		if (ids != null) {
			for (long id : ids) {
				tcomplaintService.remove(id);
			}
		}
		return "redirect:v_tcomplaintList.do";
	}

	/**
	 * 民意警务网上投诉编辑页面
	 * 
	 * @param id
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tcomplaintEdit.do")
	public String edit(long id, HttpServletRequest request, ModelMap model) {
		Tcomplaint tcomplaint = tcomplaintService.get(id);
		model.addAttribute("tcomplaint", tcomplaint);
      model.addAttribute("actionEdit", request.getParameter("actionEdit"));
        setDict(model);
		return "tcomplaintEdit";
	}

	/**
	 * 民意警务网上投诉详情
	 * 
	 * @param id
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tcomplaintDetail.do")
	public String detail(long id, HttpServletRequest request, ModelMap model) {
		Tcomplaint tcomplaint = tcomplaintService.get(id);
		model.addAttribute("tcomplaint", tcomplaint);
        setDict(model);
		return "tcomplaintView";
	}

	/**
	 * 民意警务网上投诉查询（分页）
	 * 
	 * @param tcomplaint
	 * @param pageNo
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tcomplaintList.do")
	public String listPage(Tcomplaint tcomplaint, Integer pageNo,
			HttpServletRequest request, ModelMap model) {
		if (pageNo == null)
			pageNo = 1;
		Pagination pagination = tcomplaintService.getTcomplaintByPage(toMap(tcomplaint),
				pageNo, CookieUtils.getPageSize(request));
		model.addAttribute("pagination", pagination);
        setDict(model);
		return "tcomplaintList";
	}

	/**
	 * 民意警务网上投诉查询
	 * 
	 * @param tcomplaint
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tcomplaintList1.do")
	public String list(Tcomplaint tcomplaint, HttpServletRequest request, ModelMap model) {
		List<Tcomplaint> list = tcomplaintService.getTcomplaint(toMap(tcomplaint));
		model.addAttribute("list", list);
        setDict(model);
		return "tcomplaintList";
	}


 	/***
	 * 将页面输入信息转成map
	 * 
	 * @param tcomplaint
	 * @return
	 */	private Map<String,Object> toMap(Tcomplaint tcomplaint){
		Map<String,Object> map=new HashMap<String, Object>();
      		if(tcomplaint.getId()!=null&&!tcomplaint.getId().equals("")){
			map.put("id", tcomplaint.getId());
		}
		if(tcomplaint.getUsername()!=null&&!tcomplaint.getUsername().equals("")){
			map.put("username", tcomplaint.getUsername());
		}
		if(tcomplaint.getComplaintcontent()!=null&&!tcomplaint.getComplaintcontent().equals("")){
			map.put("complaintcontent", tcomplaint.getComplaintcontent());
		}
		if(tcomplaint.getComplaintdate()!=null&&!tcomplaint.getComplaintdate().equals("")){
			map.put("complaintdate", tcomplaint.getComplaintdate());
		}
		if(tcomplaint.getStatus()!=null&&!tcomplaint.getStatus().equals("")){
			map.put("status", tcomplaint.getStatus());
		}      
		return map;
	}
   
	/**
	 * 设置页面所需的数据字典
	 * @param model
	 */
	private void setDict(ModelMap model){
      model.addAttribute("statusMap", DictUtils.getDictByType(DictUtils.TCOMPLAINT_STATUS));		
	}

	/**
	 * 数据校验
	 * @param model
	 */
	private WebErrors validateSave(Tcomplaint tcomplaint, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		/**if (errors.ifOutOfLength(username, "username", 1, 100)) {
			return errors;
		}  */    
		return errors;
	}

   
      
	@Resource
	private TcomplaintService tcomplaintService;   
}